﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;

namespace DIS.WindowsPhone.Toolkit
{
    /// <summary>
    /// A helper class used to log exceptions in Isolated Storage
    /// </summary>
    public static class ExceptionLogger
    {
        /// <summary>
        /// Add an exception to the log
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static bool AddException(Exception ex)
        {
            IsolatedStorageFile isolatedStore = null;
            IsolatedStorageFileStream stream = null;
            try
            {
                isolatedStore = IsolatedStorageFile.GetUserStoreForApplication();
                List<Error> errors = new List<Error>();
                var serializer = new XmlSerializer(typeof(List<Error>));

                if (isolatedStore.FileExists("Exceptions.xml"))
                {
                    stream = isolatedStore.OpenFile("Exceptions.xml", System.IO.FileMode.Open);

                    if (stream.Length > 0)
                        errors = (List<Error>)serializer.Deserialize(stream);

                    stream.Dispose();
                    isolatedStore.DeleteFile("Exceptions.xml");
                }

                stream = isolatedStore.CreateFile("Exceptions.xml");
                errors.Add(new Error(ex));
                serializer.Serialize(stream, errors);
            }
            catch
            {
                return false;
            }
            finally
            {
                if (stream != null)
                    stream.Dispose();
                if (isolatedStore != null)
                    isolatedStore.Dispose();
            }

            return true;
        }

        /// <summary>
        /// Reads the exception log and returns it as a string
        /// </summary>
        /// <returns></returns>
        public static string GetLog()
        {
            IsolatedStorageFile isolatedStore = null;
            IsolatedStorageFileStream stream = null;
            StreamReader reader = null;
            try
            {
                isolatedStore = IsolatedStorageFile.GetUserStoreForApplication();
                if (isolatedStore.FileExists("Exceptions.xml"))
                {
                    stream = isolatedStore.OpenFile("Exceptions.xml", System.IO.FileMode.Open);
                    reader = new StreamReader(stream);
                    string log = reader.ReadToEnd();
                    return log;
                }
                return String.Empty;
            }
            catch
            {
                return String.Empty;
            }
            finally
            {
                if (reader != null)
                    reader.Dispose();
                if (stream != null)
                    stream.Dispose();
                if (isolatedStore != null)
                    isolatedStore.Dispose();

            }
        }

        /// <summary>
        /// Clears the exception log
        /// </summary>
        /// <returns></returns>
        public static bool ClearLog()
        {
            IsolatedStorageFile isolatedStore = null;
            try
            {
                isolatedStore = IsolatedStorageFile.GetUserStoreForApplication();

                if (isolatedStore.FileExists("Exceptions.xml"))
                    isolatedStore.DeleteFile("Exceptions.xml");

                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                if (isolatedStore != null)
                    isolatedStore.Dispose();
            }
        }

    }
}
